<template>
	<view class="content">
		<view class="flex mt-100 chat-top-box">
			<view class="icon-back" @click="back">
				<image src="/static/images/icon_back.png" mode="aspectFill" class="img"></image>
			</view>
			<view class="user-icon">
				<image src="/static/images/Jay.jpg" mode="aspectFill" class="img"></image>
			</view>
			<view class="infon-box ml-10">
				<view class="flex">
					<view class="user-name">周杰伦</view>
				</view>
				<view class="tips-text ml-10 mt-10">今天在线</view>
			</view>
		</view>
	</view>
	<!-- 聊天内容 -->
	<view class="content chat-box">
		<view class="j-center time-text">聊天的开始~</view>
		<!-- 遍历消息列表 -->
		<view v-for="(message, index) in messages" :key="index"
			:class="['flex', message.isSender ? 'mt-30' : 'flex-end mt-30']">
			<!-- 如果不是发送者的消息 -->
			<template v-if="!message.isSender">
				<view class="info-box-2">
					<view class="chat-text-2">
						{{ message.text }}
					</view>
				</view>
				<view class="user-icon ml-30">
					<image :src="message.avatar" mode="aspectFill" class="img"></image>
				</view>
			</template>

			<!-- 如果是发送者的消息 -->
			<template v-else>
				<view class="user-icon">
					<image :src="message.avatar" mode="aspectFill" class="img"></image>
				</view>
				<view class="info-box-1 ml-30">
					<view class="chat-text-1">
						{{ message.text }}
					</view>
				</view>
			</template>
		</view>
	</view>
	<view class="flex d-box">
		<view class="input-box">
			<uv-textarea v-model="value" :maxlength="-1" autoHeight placeholder="发信息···"></uv-textarea>
		</view>
		<view class="ml-30">
			<uv-button type="success" text="确定" @click="sendMessage"></uv-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value: '',
				messages: [
					{
						text: "你好啊",
						isSender: true, // 接收的消息
						avatar: "/static/images/Jay.jpg"
					},
					{
						text: "你好你好",
						isSender: false, // 发送的消息
						avatar: "/static/images/temporary/user.jpg"
					},
					{
						text: "你真的是周杰伦吗，我的天啊",
						isSender: false, // 发送的消息
						avatar: "/static/images/temporary/user.jpg"
					},
					{
						text: "一起去唱K吗",
						isSender: true, // 接收的消息
						avatar: "/static/images/Jay.jpg"
					},
				]
			}
		},
		methods: {
			back() {
				uni.navigateBack();
			},
			// 发送信息
			sendMessage(){
				this.messages.push({
					text: this.value,
					isSender: false, // 接收的消息
					avatar: "/static/images/user.jpg"
				});
				this.value = ''
			}
		}
	}
</script>

<style scoped>
	.content {
		padding: 30rpx;
	}

	.chat-top-box {
		position: fixed;
		z-index: 999;
	}

	.chat-box {
		margin-top: 130rpx;
	}

	.info-box-1 {
		padding: 30rpx;
		background-color: #eee;
		border-radius: 30rpx;
		display: flex;
	}

	.chat-text-1 {
		font-size: 28rpx;
		color: #000;
	}

	.chat-text-2 {
		font-size: 28rpx;
		color: #fff;
	}

	.info-box-2 {
		padding: 30rpx;
		background-color: #1296db;
		border-radius: 30rpx;
	}

	.input-box {
		width: 75%;
	}

	.d-box {
		width: 100%;
		position: fixed;
		bottom: 0;
		padding: 30rpx 30rpx 50rpx 30rpx;
		background-color: #fff;
	}
</style>